<?php
require_once dirname(__FILE__) . '/Item/Interface.php';

class Storefront_Resource_Cart_Item implements Storefront_Resource_Cart_Item_Interface
{
    
    public $productId;
    public $name;
    public $price;
    public $taxable;
    public $discountPercent;
    public $qty;
    
    /**
     * Constructor
     *
     * @param Storefront_Resource_Product_Item $product
     * @param int $qty 
     */
    public function __construct(Storefront_Resource_Product_Item $product, $qty) 
    {
        
        $this->productId = (int) $product->productId;
        $this->name = $product->name;
        $this->price = (float) $product->getPrice(false, false);
        $this->taxable = $product->taxable;
        $this->discountPercent = (int) $product->discountPercent;
        $this->qty = (int) $qty;
    
    }

    /**
     * The cost of line with discount and tax.
     *
     * @return float 
     */
    public function getLineCost() 
    {
        
        $price = $this->price;
        
        if (0 != $this->discountPercent) {
            $discounted = ($price * $this->discountPercent) / 100;
            $price = round($price - $discounted, 2);
        }
        
        if ("Yes" === $this->taxable) {
            $taxService = new Storefront_Service_Taxation();
            $price = $taxService->addTax($price);
        }
        
        return $price * $this->qty;
        
    }

    
}